/**
  ******************************************************************************
  * @file    measbsp.c
  * @brief   This file provides code for the configuration
  *          of all used GPIO pins.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "measure.h"
#include "parameter.h"

void meas_timetick_init(void)
{
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM9);

  /* TIM4 interrupt Init */
  NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),MEAS_TIMETICK_IRQ_PRIORITY, 0));
  NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);

  TIM_InitStruct.Prescaler = 1000-1;    /* 0.01ms per cnt */
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 1000-1;   /* 10ms */
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM9, &TIM_InitStruct);

  LL_TIM_EnableARRPreload(TIM9);

  LL_TIM_SetClockSource(TIM9, LL_TIM_CLOCKSOURCE_INTERNAL);

  LL_TIM_SetOnePulseMode(TIM9, LL_TIM_ONEPULSEMODE_REPETITIVE);

  LL_TIM_SetTriggerOutput(TIM9, LL_TIM_TRGO_RESET);

  LL_TIM_DisableMasterSlaveMode(TIM9);

  LL_TIM_ClearFlag_UPDATE(TIM9);

  LL_TIM_EnableIT_UPDATE(TIM9);

  LL_TIM_ClearFlag_UPDATE(TIM9);

  LL_TIM_EnableCounter(TIM9);
}

//extern volatile int32_t  Ics220j_WightADCodeSrc;
//extern volatile uint16_t Ics220j_SpeedADCodeSrc;

void TIM1_BRK_TIM9_IRQHandler(void)
{
//  BaseType_t xHigherPriorityTaskWoken;

  if(LL_TIM_IsActiveFlag_UPDATE(TIM9) == 1)
  {
    LL_TIM_ClearFlag_UPDATE(TIM9);
    
    vTaskNotifyGiveFromISR(xMeasureRt_Thd, NULL);
    
//    if(xHigherPriorityTaskWoken == pdTRUE)
//    {
//      taskYIELD();
//    }
  }
}
